using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Globalization;

namespace ResourceBlender_Express {
    /// <summary>
    /// A form for selecting an application, bundles and languages before viewing resources.
    /// </summary>
    public partial class WinResourcesGridPrompt : Window {
        private ApplicationSelector applicationSelector;

        /// <summary>
        /// Gets the selected application.
        /// </summary>
        /// <value>The selected application.</value>
        public RBApplication SelectedApplication {
            get { return applicationSelector.SelectedApplications[0]; }
        }
        /// <summary>
        /// Gets the selected bundles.
        /// </summary>
        /// <value>The selected bundles.</value>
        public ObservableSortedDictionary<string, string> SelectedBundles {
            get { return applicationSelector.SelectedBundles; }
        }
        /// <summary>
        /// Gets the selected languages in the format <Code, EnglishName>.
        /// </summary>
        /// <value>The selected languages.</value>
        public ObservableSortedDictionary<string, CultureInfo> SelectedLanguages {
            get {
                ObservableSortedDictionary<string, CultureInfo> selectedLangs = applicationSelector.SelectedLanguages;
                string defaultLang = applicationSelector.SelectedApplications[0].DefaultLanguage;
                if(!selectedLangs.ContainsKey(defaultLang)) {
                    selectedLangs.Add(defaultLang, Utils.GetCultureInfo(defaultLang));
                }
                return selectedLangs; 
            }
        }

        /// <summary>
        /// Initializes a new <see cref="WinResourcesGridPrompt"/>.
        /// </summary>
        public WinResourcesGridPrompt() {
            InitializeComponent();
            applicationSelector = new ApplicationSelector();
            applicationSelector.MaxHeight = 250;
            DockPanel.SetDock(applicationSelector, Dock.Top);
            dokContainer.Children.Add(applicationSelector);
            applicationSelector.IsBusy += (busy) => {
                if(busy) {
                    SetStatus("Loading applications...");
                } else {
                    SetStatus(null);
                }
            };
        }

        /// <summary>
        /// Sets the text on the status message (null hides the statusbar and label).
        /// </summary>
        /// <param name="message">The message.</param>
        private void SetStatus(string message) {
            if(message == null) {
                prgStatus.Visibility = Visibility.Hidden;
                lblStatus.Visibility = Visibility.Hidden;
            } else {
                prgStatus.Visibility = Visibility.Visible;
                lblStatus.Visibility = Visibility.Visible;
                lblStatus.Content = message;
            }
        }

        /// <summary>
        /// Validates the user's selection then returns a DialogResult of true
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void ViewResourcesHandler(object sender, RoutedEventArgs e) {
            if(applicationSelector.ValidateSelection(true)) {
                this.DialogResult = true;
            }
        }

        /// <summary>
        /// Closes the window.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void CloseWindow(object sender, RoutedEventArgs e) {
            Close();
        }
    }
}
